package com.archie.xunji.map.discover;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.archie.xunji.MainActivity;
import com.archie.xunji.R;
import com.archie.xunji.map.Constans;
import com.archie.xunji.map.SetOnGetResult;
import com.archie.xunji.map.entity.KindResultEntity;
import com.archie.xunji.map.tools.HttpAsyncTask;
import com.archie.xunji.map.tools.ParseJsonUtls;

import java.util.HashMap;
import java.util.List;

public class SearchActivity extends Activity implements SetOnGetResult, View.OnClickListener {

    private EditText input;
    
    private LinearLayout container;
    private HttpAsyncTask updateMapTask;

    private List<KindResultEntity> datas;
    private KindResultEntity entity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        
        input = (EditText) findViewById(R.id.mid_search_top_ctivity);
        container = (LinearLayout) findViewById(R.id.container_search_activity);
        
        //初始化顶部栏下面的布局
        initContainerData();
    
    }

    private void initContainerData() {

        // 网络请求，获取数据，更新地图
        String url = Constans.URL_KIND;

        HashMap<String,String> params = new HashMap<String, String>();
        params.put("userinfoid","-1");
        params.put("appuserid","43502");
        params.put("t","20151110164043");
        params.put("token","AC672236E6663BAB6E4564F6C5A57C4AE8FE67CA");
        params.put("sid","mnScF1UJh4");
        params.put("imei","4A061818D7F062836A3A279E0B99111B");
        params.put("os","Android+4.2.2");
        params.put("key","WANDOUJIA");

        getData(url, params, "utf-8");
        
    }

    private void getData(String url, HashMap<String, String> params, String encode) {
        updateMapTask = new HttpAsyncTask(params,encode,this);
        updateMapTask.execute(url);
    }

    public void btnBack(View view) {
        finish();
    }

    public void btnSearch(View view) {
        String key = input.getText().toString();
        
        if(key.length()>0){

            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("key",key);
            startActivity(intent);
            
        }
    }

    @Override
    public void onGetResult(String result) {
        Log.d("debug",result);
        if(result != null) {
            datas = ParseJsonUtls.parseKindResultEntity(result);
            
            // 更新顶部栏下面的多个按钮
            updateSurface();
        }
    }

    private void updateSurface() {
        for (KindResultEntity data : datas) {
            Log.d("debug", data.toString());
        }

        int num = datas.size() /6;
        for(int i=0;i<=num;i++){
            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.leftMargin = 15;
            params.topMargin = 15;

            for(int j=i*6;j<datas.size() && j<(i+1)*6;j++){
                entity = datas.get(j);
                TextView textView = new TextView(this);
                textView.setText(entity.getName());
                textView.setTextSize(15);
                textView.setTextColor(Color.parseColor("#ecebea"));
                textView.setBackgroundColor(Color.parseColor("#6c6c6b"));
                textView.setPadding(10, 10, 10, 10);
                textView.setClickable(true);
                textView.setTag(j);
                textView.setOnClickListener(this);


                linearLayout.addView(textView, params);
            }
            container.addView(linearLayout);
        }

    }

    @Override
    public void onClick(View v) {
        Object object = v.getTag();
        if(object != null && object instanceof Integer){
            // TODO 跳转到相关页面
            Integer posotion = (Integer) object;
            Toast.makeText(this,posotion+"",Toast.LENGTH_SHORT).show();
        }
    }
}
